<?php
	ini_set('memory_limit','128M');

	include_once("../../api/classes/gbif.species.php");
	include_once("../../api/classes/gbif.eml.php");
	include_once("../../api/classes/zip/archive.php");
	
	$testFiles = array("test1.xls");
	
	foreach($testFiles as $file) {
		print "Test File: " . $file . "<br>";
		$species = new Species( $file );
	#	print "<pre>";
		if ( $species->validateMetaData() && $species->validateSpeciesData() && $species->validateVernacularData() ) {
			print "Passed";
			print "<pre>";
			$folder = md5(mktime());
			$folder = "test";
			@mkdir("output/" . $folder);
			
			# Make EML
			$eml = new Eml();
			$worksheet = $species->getSheet(1);
			$eml->createEmlFromWorksheet( $worksheet );
			$eml->generateXml(0);
			file_put_contents("output/" . $folder . "/eml.xml", $eml->getRawXml() );
	
			# Make Meta
			file_put_contents("output/" . $folder . "/meta.xml", $species->generateSpeciesDataMeta() );
			
			$species->writeSpeciesData("output/" . $folder . "/species.csv");
			$species->writeVernacularData("output/" . $folder . "/vernacular.csv");
			$species->writeSynonymData("output/" . $folder . "/synonym.csv");
			$species->writeDistributionData("output/" . $folder . "/distribution.csv");
			$species->zipArchive(
					"output/" . $folder . "/" . $folder . ".zip"
				, "output/" . $folder . "/"
				, array("eml.xml", "meta.xml", "species.csv", "vernacular.csv", "synonym.csv", "distribution.csv")
			);
	
		} else {
			print "Failed<hr>";
			$errors = $species->getErrors();
			if (is_array($errors)) {
			foreach( $errors as $error ) {
				printf("%s (%s) - %s<br>", $error["type"], $error["code"], $error["value"]);
			}}
		}
		print "<br/><br/>";
	}
?>